package com.crm.controller;

import com.crm.entity.PageBean;
import com.crm.entity.SaleChance;
import com.crm.service.SaleChanceService;
import com.crm.util.ResponseUtil;
import com.crm.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售机会Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/saleChance")
public class SaleChanceController {

    @Resource
    private SaleChanceService saleChanceService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    @RequestMapping("/list")
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, SaleChance s_saleChance, HttpServletResponse response) throws IOException {
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("customerName", StringUtil.formatLike(s_saleChance.getCustomerName()));
        map.put("overView", StringUtil.formatLike(s_saleChance.getOverView()));
        map.put("createMan", StringUtil.formatLike(s_saleChance.getCreateMan()));
        map.put("state", s_saleChance.getState());
        map.put("devResult", s_saleChance.getDevResult());
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<SaleChance> saleChanceList = saleChanceService.find(map);

        Long total = saleChanceService.getTotal(map);

        JSONObject result = new JSONObject();
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm"));
        JSONArray jsonArray = JSONArray.fromObject(saleChanceList, jsonConfig);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    @RequestMapping("/save")
    public String save(SaleChance saleChance, HttpServletResponse response) throws IOException {
        if (StringUtil.isNotEmpty(saleChance.getAssignMan())) {
            saleChance.setState(1);
        } else {
            saleChance.setState(0); // 默认未分配状态
        }
        JSONObject jsonObject = new JSONObject();
        if (saleChance.getId() == null) {
            saleChance.setDevResult(0); // 添加的时候，默认是未开发状态
            saleChanceService.add(saleChance);
            jsonObject.put("success", true);
        } else {
            saleChanceService.update(saleChance);
            jsonObject.put("success", true);
        }
        ResponseUtil.write(response, jsonObject);
        return null;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "ids") String ids, HttpServletResponse response) throws IOException {
        String[] idsStrArr = ids.split(",");
        for (String idStr : idsStrArr) {
            saleChanceService.delete(Integer.parseInt(idStr));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", true);
        ResponseUtil.write(response, jsonObject);
        return null;
    }

    @RequestMapping("/findById")
    public String findById(@RequestParam(value = "id") String id, HttpServletResponse response) throws IOException {
        SaleChance saleChance = saleChanceService.findById(Integer.parseInt(id));

        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm"));
        JSONObject result = JSONObject.fromObject(saleChance, jsonConfig);

        ResponseUtil.write(response, result);
        return null;
    }
}
